var url = "http://127.0.0.1:8083/";

function write(obj,newListElement){
    var product = $(obj).find(".single-product-item");
    let a_image = product.find(".single-product-img").find("a");
    var price = product.find(".new-price");
    var name = product.find("h3").find("a");
    var modal = product.find(".modal-view");

    a_image.attr("data-target", newListElement["productId"]);
    product.find(".primary-image").attr("src",newListElement["productPicture"]);
    price.text("￥" + newListElement["productPrice"]);
    name.html(newListElement["productName"]);
    modal.attr("title",newListElement["productId"]);
}
function getCookie(key){
    var cookie = document.cookie;
    var s = cookie.replace("; ","=");
    while (s.indexOf(";") != -1){
        s = s.replace("; ","=");
    }
    var strings = s.replace("; ","=").split("=");
    if (strings.indexOf(key) == -1){
        return null;
    }
    return strings[strings.indexOf(key) + 1].trim();
}
window.onload=function () {
    $(function () {
        $.ajax({
            url: url + "collection/getCollectionList",
            type:"POST",
            async: false,
            data: {userName: getCookie("USER_NAME")},
            success:function (data) {
                if (data["code"] == 200) {
                    var $grid = $("#grid .row div.col-xs-12");
                    var $list = $("#list .row div.col-md-12");
                    var i = 0;
                    var j = 0;
                    var datum = data["data"];
                    $grid.each(function () {
                        if (i < datum.length){
                            $(this).attr("class","col-xs-12 col-width");
                            write(this,datum[i++]["productPo"]);
                        }else {
                            $(this).attr("class","col-xs-12 col-width hidden-sm");
                        }
                    });
                    $list.each(function () {
                        if (j < datum.length){
                            $(this).attr("class","col-md-12");
                            writeD(this,datum[j++]["productPo"]);
                        }else {
                            $(this).attr("class","col-md-12 hidden-sm");
                        }
                    });
                }else if (data["code"] == 405){
                    window.location.href=url + "user/" + data["data"] + ".html";
                }
            }
        });
    })
}